package com.edu.ahm.thread;

import java.util.concurrent.*;

/**
 * 线程创建方式三 实现Callable接口
 */
public class Thread6 implements Callable<Boolean> {

    private String url;
    private String name;

    public Thread6(String url,String name){
        this.url = url;
        this.name = name;
    }

    @Override
    public Boolean call() throws Exception {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downloader(url,name);
        System.out.println("下载的文件名为:"+name);
        return true;
    }

    public static void main(String[] args) {

        Thread6 thread1 = new Thread6("https://pic1.58cdn.com.cn/p1/big/n_v229efc92f51804ca0bde8a7899e718a72.jpg","1.jpg");
        Thread6 thread2 = new Thread6("https://pic1.58cdn.com.cn/p1/big/n_v229efc92f51804ca0bde8a7899e718a72.jpg","2.jpg");
        Thread6 thread3 = new Thread6("https://pic1.58cdn.com.cn/p1/big/n_v229efc92f51804ca0bde8a7899e718a72.jpg","3.jpg");

        // 创建执行服务
        ExecutorService service = Executors.newFixedThreadPool(3);
        // 提交执行
        Future<Boolean> result1 = service.submit(thread1);
        Future<Boolean> result2 = service.submit(thread2);
        Future<Boolean> result3 = service.submit(thread3);
        //获取结果
        try {
            Boolean r1 = result1.get();
            Boolean r2 = result2.get();
            Boolean r3 = result3.get();
            System.out.println(r1+"---"+r2+"---"+r3);

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }finally {
            //关闭服务
            service.shutdown();
        }
//        boolean rs1 = result1.get();
//        Boolean rs1 = result2.get();


    }

}
